package com.siysoft.tc.classroom.action;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.siysoft.tc.base.bean.bo.ClassroomBo;
import com.siysoft.tc.base.bean.bo.ClassroomDetailBo;
import com.siysoft.tc.base.bean.bo.ClassroomUserBo;
import com.siysoft.tc.base.bean.bo.MemberBo;
import com.siysoft.tc.base.bean.bo.PaginationBo;
import com.siysoft.tc.base.bean.bo.RoomChatBo;
import com.siysoft.tc.classroom.service.IClassroomService;
import com.siysoft.util.COMMON;
import com.tc.base.controller.BaseController;

@Controller
@RequestMapping(value = "/app/classroom")
public class ClassroomController extends BaseController{
	@Autowired
	IClassroomService classroomService;
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/loading")
	public void loading(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
				return ;
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<ClassroomBo> classroom = classroomService.getClassroom(id, pb);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("classroom", COMMON.NulltoBlank(classroom));
			obj.accumulate("pagination", pb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/detail")
	public void detail(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("classroom_id"))
					|| COMMON.isnull(request.getParameter("user_id"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
				return ;
			}
			Integer classroom_id = Integer.valueOf(request.getParameter("classroom_id").toString());
			Integer user_id = Integer.valueOf(request.getParameter("user_id").toString());
			
			ClassroomDetailBo classroom_detail = classroomService.getClassroomDetail(classroom_id, user_id);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("classroom_detail", COMMON.NulltoBlank(classroom_detail));
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/member")
	public void member(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
				return ;
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<MemberBo> member = classroomService.getMember(id, pb);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("member", COMMON.NulltoBlank(member));
			obj.accumulate("pagination", pb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/getmember")
	public void member_teacher(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("user_type"))
					|| COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
				return ;
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			Integer user_type = Integer.valueOf(request.getParameter("user_type").toString());
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<MemberBo> member = classroomService.getMemberByType(id, pb, user_type);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("member", COMMON.NulltoBlank(member));
			obj.accumulate("pagination", pb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/agreeteacher")
	public void agreeteacher(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("classroom_id"))
					|| COMMON.isnull(request.getParameter("user_id"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
				return ;
			}
			Integer classroom_id = Integer.valueOf(request.getParameter("classroom_id").toString());
			Integer user_id = Integer.valueOf(request.getParameter("user_id").toString());
			
			Integer result = classroomService.agreeTeacher(classroom_id, user_id);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("result", result);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/memberinfo")
	public void memberinfo(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
				return ;
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			
			ClassroomUserBo memberinfo = classroomService.getMemberInfo(id);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("memberinfo", COMMON.NulltoBlank(memberinfo));
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/quit")
	public void quit(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("classroom_id"))
					|| COMMON.isnull(request.getParameter("user_id"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
				return ;
			}
			Integer classroom_id = Integer.valueOf(request.getParameter("classroom_id").toString());
			Integer user_id = Integer.valueOf(request.getParameter("user_id").toString());
			
			Integer result = classroomService.quit(classroom_id, user_id);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("result", result);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/chat_list")
	public void chat_list(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("reservation_id"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
				return ;
			}
			Integer reservation_id = Integer.valueOf(request.getParameter("reservation_id").toString());
			
			List<RoomChatBo> rcb = classroomService.chat_list(reservation_id);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("roomchat", rcb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/chat_send")
	public void chat(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("reservation_id"))
					|| COMMON.isnull(request.getParameter("user_id"))
					|| COMMON.isnull(request.getParameter("content"))
					|| COMMON.isnull(request.getParameter("to_user_name"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
				return ;
			}
			Integer reservation_id = Integer.valueOf(request.getParameter("reservation_id").toString());
			Integer user_id = Integer.valueOf(request.getParameter("user_id").toString());
			String content = request.getParameter("content").toString();
			String to_user_name = request.getParameter("to_user_name").toString();
			
			Integer result = classroomService.chat_send(reservation_id, user_id, content, to_user_name);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("result", result);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/update")
	public void update(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("school"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
				return ;
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			String school = request.getParameter("school").toString();
			String lecture = COMMON.NulltoBlank(request.getParameter("lecture").toString());
			Integer max_size = COMMON.NulltoZero(Integer.valueOf(request.getParameter("max_size").toString()));
			String address = COMMON.NulltoBlank(request.getParameter("address").toString());
			Timestamp time = Timestamp.valueOf(request.getParameter("time").toString());
			Integer period = COMMON.NulltoZero(Integer.valueOf(request.getParameter("period").toString()));
			Integer goal = COMMON.NulltoZero(Integer.valueOf(request.getParameter("goal").toString()));
			Integer wage = COMMON.NulltoZero(Integer.valueOf(request.getParameter("wage").toString()));
			Integer bonus = COMMON.NulltoZero(Integer.valueOf(request.getParameter("bonus").toString()));
			
			Integer result = classroomService.update(id, school, lecture, max_size,
					address, time, period, goal, wage, bonus);
			if(result == -1){
				this.responseFail(response, -32602, "Room size is lower than the present size!", "指定参数不符合服务接口定义");
				return ;
			}
			else if(result == -2){
				this.responseFail(response, -32602, "The lecture is not exist!", "指定参数不符合服务接口定义");
				return ;
			}
			else{
				JSONObject obj = new JSONObject();
				obj.accumulate("result", result);
				String format_json = this.FormatJson(0, "", Object2Json(obj));
				this.responseSuccess(response, format_json, 0);
				return ;
			}
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
}
